class Solution(object):
    def reorderSpaces(self, text):
        space = text.count(' ')
        t = text.split(' ')
        words = []
        for c in t:
            if c:
                words.append(c)
        n = len(words)
        if n == 1:
            return words[0] + ' ' * space
        e = space // (n - 1)
        m = space % (n - 1)
        return (' ' * e).join(words) + ' ' * m


